/*
 *  Copyright (C) 2011 Hatsites
 * 
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package org.myorg.shr.gui.io.finder;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import org.myorg.shr.gui.swg.util.logging.FileHandlerShr;

/**
 *
 * @author Hatsites
 *
 * email: Hatsites_AT_gmail.com
 * ... please remove "_AT_" from the above string to get the right email address
 */

abstract public class FinderResAbs {

    protected FinderResAbs(){

    }

    protected static String _STR_PATH_PREFIX_SYS_ =
        "org" +
        java.io.File.separator +
        "myorg";

    protected static String _STR_PATH_PREFIX_JAR =
        "org" +
        "/" +
        "myorg";


    static protected javax.swing.ImageIcon _s_getIcon_(
        ClassLoader cldSubClass,
        String[] strsFileName,
        String strFileSuffix,
        String strPathRelSys,
        String strPathRelJar)
        {
        for (int i= 0; i <strsFileName.length; ++i){
            strPathRelSys += java.io.File.separator
                    + strsFileName[i];
            strPathRelJar += "/"
                    + strsFileName[i];
        }

        if (cldSubClass==null || strsFileName==null || strPathRelSys==null || strPathRelJar==null)
        {
        	FinderResAbs._LOGGER_.severe("nil arg");
            return null;
        }

        // 1) not jarred & development
        java.io.File fle = new java.io.File(strPathRelSys + strFileSuffix);

        if (! fle.exists())
        {
            //2) jarred, development or release
            java.net.URL url = ClassLoader.getSystemResource(strPathRelJar + strFileSuffix);

            if (url == null)
            {
                //3) JavaWebStart's cache resources
                url = cldSubClass.getResource(strPathRelJar + strFileSuffix);

                if (url == null)
                {
                    FinderResAbs._LOGGER_.log(Level.SEVERE, "nil url: {0}{1}", new Object[]{strPathRelJar+strFileSuffix, strsFileName});
                	return null;
                }

            }

            return (new ImageIcon(url));
        }

        fle = null;
        // !!!!!!!!!!!!!!!!!!!!!!
        return (new ImageIcon(strPathRelSys + strFileSuffix));
    }


    final static private Logger _LOGGER_ = Logger.getLogger(FinderResAbs.class.getName());

    static
    {
        FinderResAbs._LOGGER_.addHandler(FileHandlerShr
                .s_getInstance());
    }
}
